﻿/**********************************************************
 * 
 * AjaxEngine(Houfeng.Web.Ajax) 
 * -------------------------------------------------------
 * 作者:Houfeng(侯锋)
 * Email:Houzhanfeng@gmail.com
 * 
 *********************************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace AjaxEngines
{
    [Serializable]
    public class Message
    {
        private MessageType _Type;
        private string _ID;
        private string _Context;
        /// <summary>
        /// 构造一个消息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="id"></param>
        /// <param name="context"></param>
        public Message(MessageType type, string id, string context)
        {
            this._Type = type;
            this._ID = id;
            this._Context = context;
        }
        /// <summary>
        /// 构造一个消息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="context"></param>
        public Message(MessageType type,string context)
        {
            this._Type = type;
            this._ID = type.ToString().ToLower();
            this._Context = context;
        }
        /// <summary>
        /// 消息类型
        /// </summary>
        public MessageType Type
        {
            get
            {
                return this._Type;
            }
            set
            {
                this._Type = value;
            }
        }
        /// <summary>
        /// 消息ID
        /// </summary>
        public string ID
        {
            get
            {
                return this._ID;
            }
            set
            {
                this._ID = value;
            }
        }
        /// <summary>
        /// 消息内容
        /// </summary>
        public string Context
        {
            get
            {
                return this._Context;
            }
            set
            {
                this._Context = value;
            }
        }
        /// <summary>
        /// 输出Json字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string rs = ("{\"type\":\"" + FilterString(this._Type.ToString().ToLower()) + "\",\"id\":\"" + FilterString(this.ID) + "\",\"context\":\"" + FilterString(this._Context) + "\"}");
            return rs;
        }
        /// <summary>
        /// 过滤字符串，用于向客户端输出
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FilterString(string str)
        {
            if (str != null)
                return str.Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\r", "\\r").Replace("\n", "\\n");
            else
                return "";
        }
        /// <summary>
        /// 对应js中的escape
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private string escape(string s)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                byte[] ba = System.Text.Encoding.Unicode.GetBytes(s);
                for (int i = 0; i < ba.Length; i += 2)
                {
                    sb.Append("%u");
                    sb.Append(ba[i + 1].ToString("X2"));
                    sb.Append(ba[i].ToString("X2"));
                }
                return sb.ToString();
            }
            catch
            {
                return null;
            }

            
        }

        
    }
    
}
